package com.sahyadri.web.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * Date/Time utility class
 * 
 * 
 */
public class DateTimeUtil {

	private static final String STARTING_TIME = "12:00AM";
	private static final String AM = "AM";
	private static final String PM = "PM";
	private static final String DOB_FORMAT = "dd-MM-yyyy";

	public static final String DEFAULT_DATE_FORMAT = "dd/MM/yyyy";
	public static final String DEFAULT_DATE_TIME_FORMAT = "	dd/MM/yyyy HH:mm:ss";
	public static final String DEFAULT_DATE_TIME_LOCALE_FORMAT = " dd/MM/yyyy HH:mm z";
	static final Locale locale = new Locale("ENG", "IND") ;
	
	/**
	 * Builds the time list.
	 * 
	 * @param incMins
	 *            the minutes increment
	 * 
	 * @return the list< string> containing the list of time intervals.
	 */
	public static List<String> buildTimeList(int incMins) {
		ArrayList<String> lstTimeList = new ArrayList<String>();
		lstTimeList.add(STARTING_TIME);

		int hr = 0;
		int min = 0;
		int displayHr = 0;
		String sHr = "";
		String sMin = "";
		String strTimeItem = ""; // initial value
		String amPM = AM;
		do {
			min = min + incMins;
			if (min >= 60) {
				hr = hr + 1;
				displayHr = hr;
				if (hr >= 12) {
					displayHr = hr - 12;
					if (displayHr == 0)
						displayHr = 12;
					amPM = PM;
				}
				min = min - 60;
			}
			// Prefix 0 as needed
			sHr = String.valueOf(displayHr);
			if (displayHr < 10) {
				sHr = "0" + sHr;
			}
			sMin = String.valueOf(min);
			if (min < 10) {
				sMin = "0" + sMin;
			}
			strTimeItem = sHr + ":" + sMin + amPM;
			lstTimeList.add(strTimeItem);
		} while (hr < 23);
		return lstTimeList;
	}

	/**
	 * StringToDate
	 * 
	 * @param formatString
	 *            the date format string
	 * @param dateString
	 *            the date string to be converted
	 * 
	 * @return the converted date
	 * @throws ParseException
	 */
	public static Date stringToDate(String formatString, String dateString) throws ParseException {
		Date returnDate = null;
		DateFormat df = null;
		if (formatString != null) {
			df = new SimpleDateFormat(formatString);
			returnDate = df.parse(dateString);
		}
		return returnDate;
	}

	/**
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		String retDate = "";
		if (date != null) {
			DateFormat df = new SimpleDateFormat(DEFAULT_DATE_TIME_LOCALE_FORMAT, locale);
			retDate = df.format(date);
		}
		return retDate;
	}

	public static Date xmlGregorianCalendarToDate(XMLGregorianCalendar xmlCal) {
		if (xmlCal == null)
			return null;
		return xmlCal.toGregorianCalendar().getTime();
	}

	public static String xmlGregorianCalendarToString(XMLGregorianCalendar xmlCal) {
		if (xmlCal == null)
			return null;
		return changeDateFormat(xmlCal.toGregorianCalendar().getTime(), DEFAULT_DATE_FORMAT);
	}
	
	public static String xmlGregorianCalendarToString(XMLGregorianCalendar xmlCal, String format) {
		if (xmlCal == null)
			return null;
		return changeDateFormat(xmlCal.toGregorianCalendar().getTime(), format);
	}

	public static XMLGregorianCalendar getXmlGregorianCaledarDate(Date date) {
		try {
			if (date == null) {
				return null;
			}
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(date);
			XMLGregorianCalendar currentDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
			return currentDate;
		} catch (Exception e) {
			return null;
		}
	}

	public static String changeDateFormat(String date, String currentDateFormat, String expectedDateFormat) {
		String expectedDate = "";
		try {
			if (date != null && !date.equals("")) {
				SimpleDateFormat formatter1 = new SimpleDateFormat(currentDateFormat);
				Date currentDate = formatter1.parse(date);

				SimpleDateFormat formatter2 = new SimpleDateFormat(expectedDateFormat);
				expectedDate = formatter2.format(currentDate);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return expectedDate;
	}

	public static String changeDateFormat(Date date, String expectedDateFormat) {
		String expectedDate = "";
		try {
			if (date != null) {

				SimpleDateFormat formatter = new SimpleDateFormat(expectedDateFormat, Locale.US);
				expectedDate = formatter.format(date);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return expectedDate;
	}

	public static Date changeFormatInDate(Date date, String expectedDateFormat) {
		GregorianCalendar expectedDate = new GregorianCalendar();
		try {
			if (date != null) {

				SimpleDateFormat formatter = new SimpleDateFormat(expectedDateFormat);
				expectedDate.setTime(formatter.parse(formatter.format(date)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return expectedDate.getTime();
	}

	/**
	 * 
	 * @param dob
	 * @return
	 * @throws ParseException
	 */
	public static XMLGregorianCalendar getXmlGregorianCalendarDate(String strDate, String dateFormat)
			throws ParseException {
		Date date = stringToDate(dateFormat, strDate);
		return getXmlGregorianCaledarDate(date);
	}

	public static void main(String[] args) throws Exception{
		/*GregorianCalendar expectedDate = new GregorianCalendar(TimeZone.getTimeZone("IST"), locale);
		System.out.println(expectedDate.getTimeZone());
		Date date = expectedDate.getTime();
		System.out.println(date);
		System.out.println(dateToString(date));*/
		  
		Date date = stringToDate(DateTimeUtil.DEFAULT_DATE_FORMAT, "22/01/2012" );
		 System.out.println(date);
		 System.out.println(date.getTime());
		 String string1 = "2009-10-10 12:12:12";
		  DateFormat firstFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  DateFormat thirdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  TimeZone firstTime = TimeZone.getTimeZone("IST");
		  TimeZone secondTime = TimeZone.getTimeZone("GMT");
		  TimeZone thirdTime = TimeZone.getTimeZone("PST");
		  firstFormat.setTimeZone(firstTime);
		  secondFormat.setTimeZone(secondTime);
		  thirdFormat.setTimeZone(thirdTime);
		  System.out.println("-->IST: " + firstFormat.format(date));
		  System.out.println("-->GMT: " + secondFormat.format(date));
		  System.out.println("-->PST: " + thirdFormat.format(date));
		  
		  System.out.println("-->IST: " + firstFormat.parse(string1));
		  System.out.println("-->GMT: " + secondFormat.parse(string1));
		  System.out.println("-->PST: " + thirdFormat.parse(string1));
		
		/*Date date = new Date();  
		   
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");  
		formatter.setTimeZone(TimeZone.getTimeZone("PST"));  
		   
		// Prints the date in the CET timezone  
		System.out.println(formatter.format(date));  
		   
		// Set the formatter to use a different timezone  
		formatter.setTimeZone(TimeZone.getTimeZone("IST"));  
		   
		// Prints the date in the IST timezone  
		System.out.println(formatter.format(date));  
		System.out.println(formatter.parse("21/01/2012 17:31:36 PST"));  
		
		Calendar startDateCalendar = new GregorianCalendar(new Locale("ENG", "IND"));
		startDateCalendar.setTime (date);
		startDateCalendar.setTimeZone(TimeZone.getTimeZone("IST"));
		System.out.println(startDateCalendar.getTime());*/
		
		Calendar localTime = Calendar.getInstance();
		localTime.setTime(new Date());
       /* localTime.set(Calendar.HOUR, 17);
        localTime.set(Calendar.MINUTE, 15);
        localTime.set(Calendar.SECOND, 20);*/

        int hour = localTime.get(Calendar.HOUR);
        int minute = localTime.get(Calendar.MINUTE);
        int second = localTime.get(Calendar.SECOND);


        // Print the local time
        System.out.printf("Local time  : %02d:%02d:%02d\n", hour, minute, second);


        // Create a calendar object for representing a Germany time zone. Then we
        // wet the time of the calendar with the value of the local time

        Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("IST"));
        germanyTime.setTime(new Date());
      //  germanyTime.setTimeInMillis(localTime.getTimeInMillis());
        hour = germanyTime.get(Calendar.HOUR);
        minute = germanyTime.get(Calendar.MINUTE);
        second = germanyTime.get(Calendar.SECOND);


        // Print the local time in Germany time zone
        System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
		
		
	}
}
